using System;
using System.Collections.Generic;
using System.Text;
using MSOutlook = Microsoft.Office.Interop.Outlook;
using OfficeCore = Microsoft.Office.Core;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using SharePointExplorer;

namespace OutlookSharePointExplorer
{
    class OutlookMailItem : OutlookInspector
    {

        #region  Variables

        // variables for our ToolBar
        OfficeCore.CommandBar _CommandBar = null;
        OfficeCore.CommandBarButton _Button = null;

        // the MailItem ( the Data )
        MSOutlook.MailItem _Item; 

        #endregion

        public OutlookMailItem(object item)
        {
            // cast to MailItem obect
            _Item = (MSOutlook.MailItem)item;

            //string parent = OutlookHelper.GetParentFolderPath(_Item);

            // did we have a new Item ?
            _IsNew = (_Item.Size == 0);

            // register for the _Item_Open event
            _Item.Open += new MSOutlook.ItemEvents_10_OpenEventHandler(_Item_Open);
        }

        void _Item_Open(ref bool Cancel)
        {
            // get the Inspector for this Item
            _Inspector = (MSOutlook.InspectorClass) _Item.GetInspector;
            
            // now call the ItemOpened Method
            // !!!! Don't forget !!!!!
            base.ItemOpened();

            // Initialize everything
            CreateToolBar();

            // we are finished initialize Controls...
            _IsInitializing = false;
        }

        /// <summary>
        /// Adds the Menu for the MailItem
        /// </summary>
        private void CreateToolBar()
        {
            string ToolBarName = string.Empty;
            try
            {
                if (_Inspector == null) return;

                ToolBarName = Constantes.COMMAND_BAR_NAME_NEW_MAIL;
                // check, if there is already our CommandBar
                foreach (OfficeCore.CommandBar bar in _Inspector.CommandBars)
                {
                    if (bar.Name == ToolBarName)
                    {
                        // we found our ToolBar
                        _CommandBar = bar;

                        // remove the Controls
                        foreach (OfficeCore.CommandBarControl ctrl in _CommandBar.Controls)
                        {
                            ctrl.Delete(false);
                        }
                        break;
                    }
                }

                // If we havn't found our ToolBar, add our Own CommandBar to MailItem
                if (_CommandBar == null)
                {
                    _CommandBar = _Inspector.CommandBars.Add(ToolBarName, _Missing, _Missing, true);
                }

                // Make Menu Visible in TOP of Menus
                _CommandBar.Visible = true;
                _CommandBar.Enabled = true;
                _CommandBar.Position = OfficeCore.MsoBarPosition.msoBarTop;

                // Add _ button
                _Button = (OfficeCore.CommandBarButton)_CommandBar.Controls.Add(OfficeCore.MsoControlType.msoControlButton, 1, _Missing, 1, 1);
                _Button.Caption = Constantes.COMMAND_BAR_BUTTON_NAME_NEW_MAIL;
                _Button.Tag = _ID.ToString();
                _Button.Style = OfficeCore.MsoButtonStyle.msoButtonIconAndCaption;
                // Use one off 2 zilliards Office Icons....
                _Button.FaceId = 24; // Email

                // Register for Click event
                _Button.Click +=new OfficeCore._CommandBarButtonEvents_ClickEventHandler(_Button_Click);

            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

         /// <summary>
        /// The ResetButton Click eventhandler
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="CancelDefault"></param>
        private void _Button_Click(OfficeCore.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            string mySubject = string.Empty;
            string SharePointZipFilePath = string.Empty;
            string SharePointDisplayName = string.Empty;
            ZipandEmailForm obj = null;
            try
            {
                // Check, if we are in the right MailItem
                if (Ctrl.Tag != _ID.ToString()) return;

                obj = new ZipandEmailForm();
                if (obj.ShowDialog() == DialogResult.OK)
                {
                    if (obj.MyZipFilePath.Trim().Length > 0)
                    {
                        SharePointDisplayName = "SharePointDocumentsZipped_" + (_Item.Attachments.Count + 1);
                        SharePointZipFilePath = obj.MyZipFilePath;
                        _Item.Attachments.Add(SharePointZipFilePath, MSOutlook.OlAttachmentType.olByValue, 1, SharePointDisplayName);
                   }
                 }
                
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                mySubject = null;
                obj = null;
                SharePointZipFilePath = null;
                SharePointDisplayName = null;
            }


        }

        /// <summary>
        /// Closed 
        /// </summary>
        protected override void OnClosed()
        {
            _Button.Click -= new OfficeCore._CommandBarButtonEvents_ClickEventHandler(_Button_Click);

            if (_CommandBar != null)
            {
                _CommandBar.Delete();
            }
        }

    }
}
